#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/11/13 10:54
# @Author  : Yunhao.Cao
# @File    : spider.py
from __future__ import unicode_literals
import re
from logger import Logger

__author__ = 'Yunhao.Cao'

__ALL__ = ["CustomSpider", ]


class Spider(object):
    class Rule:
        def __init__(self, url, func):
            self.url = url
            self.func = func

    _rule_list = []

    _rule_urls = None

    @classmethod
    def parse(cls, url, text, **kwargs):
        """
        根据对应的解析规则解析页面
        :param url: 访问的url
        :param text: 网页源代码
        :param kwargs: 其他参数
        :return:
        """
        func = cls._match_url(url)

        if func is None:
            return

        if isinstance(func, (staticmethod,)):
            func = func.__func__

        return func(text, **kwargs)

    @classmethod
    def check_url(cls, url):
        return cls._match_url(url) is not None

    @classmethod
    def _match_url(cls, url):
        """
        检测是否只有一个规则与之匹配
        :param url:
        :return: function 解析函数
        """
        match_count = 0
        func = None

        for rule in cls._rule_list:
            if re.match(rule.url, url):
                func = rule.func
                match_count += 1

        if match_count < 1:
            Logger.error("Unknown url ({})".format(url))
        elif match_count > 1:
            Logger.error("Wrong rules. Match results more than 1. ({})".format(url))
            raise

        return func


if __name__ == '__main__':
    pass
